<template>
  <div class="tab-bar-item" @click="itemClick">
    <!-- 所有的图片都展示同一个文字，同一个标签  -->
      <div v-if="!isActive"><slot  name="item-icon"></slot></div>
      <div v-else><slot name="item-icon-active"></slot></div>
      <!-- 用div包住标签，从而改变颜色 -->
      <div :style="activeStyle"><slot name="item-text"></slot></div>
    </div>
</template>

<script>
export default {
  name:"TabBarItem",
  props:{
    path:String,
    activeColor:{
      type:String,
      default:'pink'
    }
  },
  data(){
    return{
    //  isActive:true
    }
  },
  computed:{
  isActive(){
    // /home -> item1(/home) =true
    // /home -> item1(/category) =false
    // /home -> item1(/cart) =false
    // /home -> item1(/profile) =false
    return this.$route.path.indexOf(this.path) !==-1 //!==-1 是指找到当前的this.path
  },
  activeStyle(){
    return this.isActive ? {color:this.activeColor} : {}
  }
  },
  methods:{
    itemClick(){
      this.$router.replace(this.path)
    }
  }

}
</script>

<style>

.tab-bar-item{
  flex:1;
  text-align: center;
  height:49px;
  font-size: 14px;
}
.tab-bar-item img{
  width: 24px;
  height: 24px;
  margin-top:2px;
  vertical-align: middle;
}

</style>
